<?php
	include './header.php';

	//cause:header.php中用过$list遍历过了,so重新用一个变量遍历
	$stemp=$list;
	foreach ($stemp as $k => $v) 
	{
		$sql1="select `name`,`id` from `category` where pid=".$v['id']." and status=1";
		$res=$model->find($sql1);
		$stemp[$k]['son']=$res;
		//var_dump($stemp);exit;
	}

	$sql_goods="select
					g.id        gid,
					g.name      gname,
					g.is_new    gnew,
					g.is_hot    ghot,
					g.is_bast   gbast,
					g.price     gp,
					g.stock     gs,
					gi.name     giname,
					c.name      cname,
					g.addtime   gtime
				from
					goods       g,
					goods_img   gi,
					category    c
				where
					g.id=gi.goods_id
				and
					g.cate_id=c.id
				and
					gi.is_cover=1
				and
					g.is_bast=1
				";
	$new=[];
	$hot=[];
	$num=1;
	$goods_list=$goods_model->find($sql_goods);
	if (!empty($goods_list)) 
	{
		foreach ($goods_list as $k => $v) 
		{
			if ($num>4) 
			{
				break;
			}
			if ($v['gnew']==1) 
			{
				$new[]=$v;
			}
			if ($v['ghot']==1) 
			{
				$hot[]=$v;
			}
			$num++;
		}
	}
	if (!empty($new)) 
	{
		foreach ($new as $key => $value) 
		{
			$new[$key]['giname']=URL.'/public/uploads/'.substr($value['giname'], 0,4).'/'.substr($value['giname'], 4,2).'/'.substr($value['giname'], 6,2).'/400_'.$value['giname'];
		}
	}
	if (!empty($hot)) 
	{
		foreach ($hot as $ke => $val) 
		{
			$hot[$ke]['giname']=URL.'/public/uploads/'.substr($val['giname'], 0,4).'/'.substr($val['giname'], 4,2).'/'.substr($val['giname'], 6,2).'/300_'.$val['giname'];
		}
	}

	//遍历广告的语句
	$ad_sql="select `name`,`location` from `advertisement` where status=1";
	$ad_list=$model->find($ad_sql);
	$adarray=array();
	foreach ($ad_list as $key => $value) 
	{
		if ($value['location']==1) 
		{
			$adarray['name']=URL.'/public/uploads/'.substr($value['name'], 0,4).'/'.substr($value['name'], 4,2).'/'.substr($value['name'], 6,2).'/'.$value['name'];
		}
	}
?>
<div class="clear"></div>
<div id="main">
	<div class="main_left">
		<ul>
		<?php foreach ($stemp as $ke => $val) :?>
			<li>
				<h3><?php echo $val['name'];?></h3>
				<?php 
					if (!empty($val['son'])) :?>
						<?php foreach ($val['son'] as $va) :?>
							<span><?php echo $va['name'];?></span>
						<?php endforeach;?>
					<?php else :?>
						<span></span>
				<?php endif;?>
			</li>
		<?php endforeach;?>
		</ul>
	</div>
	<div class="main_right">
		<img src="<?php echo $adarray['name'];?>" style="margin:10px 20px;">
	</div>
	<div class="clear"></div>

	<!--遍历新品图片-->
	<div class="new_nav">
		<span>新品上市</span>
		<a href="<?php echo URL;?>/new.php">More</a>
	</div>
	<div class="new">
		<?php if (!empty($new)) :?>
			<ul>
				<?php foreach ($new as $n => $va) :?>
					<a href="<?php echo URL;?>/detail.php?id=<?php echo $va['gid'];?>"><li><img src="<?php echo $va['giname'];?>" alt=""></li></a>
				<?php endforeach;?>
			</ul>
		<?php else :?>
			<div>没有新品上架</div>
		<?php endif;?>
	</div>
	<div class="clear"></div>
	<!--遍历热销商品-->
	<div class="hot_nav">
		<span>热销商品</span>
		<a href="<?php echo URL;?>/hot.php">More</a>
	</div>
	<div class="hot">
		<?php if (!empty($hot)) :?>
			<ul>
				<?php foreach ($hot as $h => $ho) :?>
					<a href="<?php echo URL;?>/detail.php?id=<?php echo $ho['gid'];?>"><li>
						<img src="<?php echo $ho['giname'];?>">
					</li></a>
				<?php endforeach;?>			
			</ul>
		<?php else :?>	
				<div>没有热销商品</div>
		<?php endif;?>
	</div>
	<div class="clear"></div>
</div>

<?php 
	include PATH.'/footer.php';
?>